<?php

class Response
{
    // CONTENT TYPE
    const JSON = 'application/json';
    const HTML = 'text/html';
    const JAVASCRIPT = 'text/javascript';
    const JS   = 'text/javascript';
    const TEXT = 'text/plain';
    const XML  = 'text/xml';

    static public $response_type = null;

    static public function Default_JSON($code, $data = array(), $content_type = Response::JSON)
    {
        self::$response_type = Response::JSON;

        if (is_object($data))
            $data = get_object_vars($data);
        else if (! is_array($data))
            $data = array();

        if($code != 0){
            $data['retCode'] = $code;
        }

        if(isset($_REQUEST['jsonp_callback'])){
            return $_REQUEST['jsonp_callback'] . '(' . json_encode($data) . ')';
        }else{
            if($content_type!==null){
                header("Content-type: " . $content_type);
            }
            return json_encode($data);
        }
    }

    static public function JSON($code, $data = array(), $content_type = Response::JSON)
    {
        self::$response_type = Response::JSON;

        if (is_object($data))
            $data = get_object_vars($data);
        else if (! is_array($data))
            $data = array();

        $data['retCode'] = $code;

        if(isset($_REQUEST['jsonp_callback'])){
            return $_REQUEST['jsonp_callback'] . '(' . htmlspecialchars(json_encode($data), ENT_NOQUOTES) . ')';
        }else{
            if($content_type!==null){
                header("Content-type: " . $content_type);
            }
            return htmlspecialchars(json_encode($data), ENT_NOQUOTES);
        }
    }

    static public function HTML($code, $data = array(), $content_type = Response::JSON)
    {
        self::$response_type = Response::HTML;

        if (is_object($data))
            $data = get_object_vars($data);
        else if (! is_array($data))
            $data = array();

        $data['retCode'] = $code;

        if(isset($_REQUEST['jsonp_callback'])){
            return $_REQUEST['jsonp_callback'] . '(' . json_encode($data) . ')';
        }else{
            header("Content-type: " . $content_type);

            return json_encode($data);
        }
    }

    static public function format($data,$format='json',$format_setting){
        $charset =  'utf-8';

        if(strtolower($format) == 'json'){
            header("Content-type: " . Response::JSON.';charset=utf-8');
            return json_encode($data);
        }elseif(strtolower($format) == 'xml'){
            header("Content-type: " . Response::XML.';charset='.$charset);
            $xml_setting = array(
                'root'=>'response',
                'charset'=>$charset,
                'num_key'=>'item',
                'version'=>'1.0'
            );
            $format_setting = is_array($format_setting) ? $format_setting : array();
            $xml_setting    = array_merge($xml_setting,$format_setting);
            return toXml($data,$xml_setting);
        }else{
            header("Content-type: " . Response::JAVASCRIPT.';charset=utf-8');
            return $format.'('.json_encode($data).');';
        }
    }
}

?>